package com.leigang.boot.redis.springbootredis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    @Qualifier("adminRedisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 设置值  无过期时间
     *
     * @param key key
     * @param value value
     */
    public void setObj(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }


    /**
     * 设置值 有过期时间
     *
     * @param key key
     * @param value value
     */
    public void setObjs(String key, Object value) {
        redisTemplate.opsForValue().set(key, value, 60 * 20, TimeUnit.SECONDS);
    }

    /**
     * 获取值
     *
     * @param key key
     * @return obj
     */
    public Object getObj(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除值
     *
     * @param key key
     * @return boolean
     */
    public Boolean delObj(String key) {
        return redisTemplate.delete(key);
    }

}
